package com.fashare.activitytracker.slice;

import com.fashare.activitytracker.ResourceTable;
import com.fashare.activitytracker.TrackerService;
import ohos.aafwk.ability.AbilityMissionInfo;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.accessibility.AccessibilityEventInfo;
import ohos.agp.components.*;
import ohos.agp.components.element.Element;
import ohos.agp.window.service.Window;
import ohos.agp.window.service.WindowManager;
import ohos.bundle.ElementName;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Handler;
import java.util.logging.LogRecord;

public class MainAbilitySlice extends AbilitySlice {
    private boolean onlyOne=false;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_main);
        Component componentById = findComponentById(ResourceTable.Id_btn);

        //点击开始悬浮窗
        componentById.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if(!onlyOne) {
                    onlyOne=!onlyOne;
                    ComponentContainer parse = (DirectionalLayout) LayoutScatter.getInstance(MainAbilitySlice.this).parse(ResourceTable.Layout_layout_floating, null, false);
                    addPlayerView(parse, initParams());
                    return;
                }
                if(AccessibilityUtil.checkAccessibility(MainAbilitySlice.this)) {
                    System.out.println("guo----121212");
                }
                Operation operation= new Intent.OperationBuilder().withAbilityName(TrackerService.class.getName())
                        .withBundleName(getBundleName()).build();
                Intent intent1=new Intent();
                intent1.setOperation(operation);
                intent1.setParam(TrackerService.COMMAND,TrackerService.COMMAND_OPEN);
                startAbility(intent1);
                System.out.println("guo----"+intent1);
                onStop();
            }
        });



    }


    private void addPlayerView(ComponentContainer component, WindowManager.LayoutConfig param) {
        System.out.println("------------111------------");
        WindowManager windowManager = WindowManager.getInstance();
        Window window = windowManager.addComponent(component, this, WindowManager.LayoutConfig.MOD_APPLICATION_OVERLAY);
        param.alpha = 1;
//        param.type = WindowManager.LayoutConfig.MOD_APPLICATION_OVERLAY;
        window.setLayoutConfig(param);

        window.addWindowFlags(WindowManager.LayoutConfig.MOD_APPLICATION_OVERLAY);
        window.addFlags(WindowManager.LayoutConfig.MOD_APPLICATION_OVERLAY);
        window.setTransparent(true);
        window.setMovable(true);
        window.setWindowLayout(800,200);
        window.setLayoutInDisplaySideMode(0);
    }

    private WindowManager.LayoutConfig initParams() {
        WindowManager.LayoutConfig paramClose = new WindowManager.LayoutConfig();
//        paramClose.width = ComponentContainer.LayoutConfig.MATCH_CONTENT;
//        paramClose.height = ComponentContainer.LayoutConfig.MATCH_CONTENT;
        paramClose.type = WindowManager.LayoutConfig.MOD_APPLICATION_OVERLAY;
        paramClose.flags = WindowManager.LayoutConfig.MARK_FOCUSABLE_IMPOSSIBLE
                | WindowManager.LayoutConfig.MARK_WATCH_OUTSIDE_TOUCH
                | WindowManager.LayoutConfig.LAYOUT_IN_DISPLAY_SIDE_MODE_NEVER;
        return paramClose;
    }
    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
